#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define TaskNum 3

typedef void (*task_t)();

void task1()
{
    std::cout << "I am task1" << std::endl;
}

void task2()
{
    std::cout << "I am task2" << std::endl;
}

void task3()
{
    std::cout << "I am task3" << std::endl;
}

task_t Tasks[TaskNum];

void LoadTask()
{
    srand(time(nullptr)^getpid()^17777);
    Tasks[0] = task1;
    Tasks[1] = task2;
    Tasks[2] = task3;
}

void work()
{
    int number = -1;
    while(true)
    {
        int i = read(0, &number, sizeof(number));
        if(i != 0)
        {
            std::cout << "work begin: I am process " << getpid() <<" ";
            Tasks[number]();
        }
        else
        {
            break;
        }
    }
}

int Select()
{
    return rand() % TaskNum;
}

int SelectChannel(int channelnum)
{
    static int n = 0;
    int channel = n;
    n++;
    if(n == channelnum)
        n %= channelnum;

    return channel;
}